home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / eedsrc24.zip / EELIBSRC.ZIP / EELIB.C next >
C/C++ Source or Header  |  1992-11-04  |  9KB  |  406 lines

  1. #include <stdio.h>
  2. #include "eelib.h"
  3. #include "EElibdat.h"
  4.  
  5. #define VER "EELib, Library Manager,    Version 1.0"
  6. #define DEL_FILE "del"        /* Deleted file extension */
  7. #define SCRATCH_FILE "eelib.del"
  8.  
  9.  
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.     if(argc==1){    /* no command line arg's givern */
  15.         Command_Screen();
  16.     } else {    /* Parrs the commandline */
  17.         printf("%s\n",VER);
  18.         if(argc!=3 & argc!=4){
  19.             usage();
  20.         } else {
  21.  
  22.             if(argv[1][0]=='-' || argv[1][0]=='\/'){
  23.  
  24.             switch (argv[1][1]){
  25.                 case 'a' :
  26.                 case 'A' :
  27.                     if(argc!=4)
  28.                         usage();
  29.                     lib_cmd=1;
  30.                     sprintf(file_in,"%s.lib",argv[2]);
  31.                     sprintf(file_out,"%s",argv[3]);
  32.                     break;
  33.                 case 'c' :
  34.                 case 'C' :
  35.                     if(argc!=3)
  36.                         usage();
  37.                     lib_cmd=2;
  38.                     sprintf(file_in,"%s.lib",argv[2]);
  39.                     break;
  40.                 case 'd' :
  41.                 case 'D' :
  42.                     if(argc!=4)
  43.                         usage();
  44.                     lib_cmd=3;
  45.                     sprintf(file_in,"%s.lib",argv[2]);
  46.                     sprintf(file_out,"%s",argv[3]);
  47.                     break;
  48.                 case 'e' :
  49.                 case 'E' :
  50.                     if(argc!=4)
  51.                         usage();
  52.                     lib_cmd=4;
  53.                     sprintf(file_in,"%s.lib",argv[2]);
  54.                     sprintf(file_out,"%s",argv[3]);
  55.                     break;
  56.                 case 'l' :
  57.                 case 'L' :
  58.                     if(argc!=4)
  59.                         usage();
  60.                     lib_cmd=5;
  61.                     sprintf(file_in,"%s.lib",argv[2]);
  62.                     sprintf(file_out,"%s.lst",argv[3]);
  63.                     break;
  64.                 case 'm' :
  65.                 case 'M' :
  66.                     if(argc!=4)
  67.                         usage();
  68.                     lib_cmd=7; 
  69.                     sprintf(file_in,"%s.lib",argv[2]);
  70.                     sprintf(file_out,"%s.map",argv[3]);
  71.                     break;
  72.                 case 's' :
  73.                 case 'S' :
  74.                     if(argc!=3)
  75.                         usage();
  76.                     lib_cmd=6;
  77.                     sprintf(file_in,"%s.lib",argv[2]);
  78.                     break;
  79.                 default :
  80.                     usage();
  81.                 }
  82.             command();
  83.            } else {
  84.             usage();
  85.            }
  86.         }
  87.     }
  88.     return(0);
  89. }
  90. void usage(void)
  91. {
  92.     printf("EELib <Switch> <File1> <File2>\n");
  93.     exit(-1);
  94. }
  95. void Command_Screen(void)
  96. {
  97.     printf("No Command Screen Code Yet\n");
  98.     usage();
  99. }
  100. void command(void)
  101. {
  102.     switch(lib_cmd){
  103.         case 1 :
  104.             add_lib();
  105.             break;
  106.         case 4 :
  107.             extract_lib();
  108.             break;
  109.         case 2 :
  110.             create_lib();
  111.             break;
  112.         case 3 :
  113.             delete_lib();
  114.             break;
  115.         case 5 :
  116.             list_lib();
  117.             break;
  118.         case 6 :
  119.             show_lib();
  120.             break;
  121.         case 7 :
  122.             map_lib();
  123.             break;
  124.     }
  125. }
  126. void add_lib(void)
  127. {
  128.     char make_name[0x100];
  129.  
  130.     int found_error = 0;
  131.  
  132.     if((fin=fopen(file_in,"r"))==NULL){
  133.         fprintf(stderr,"Can't open library file %s\n",file_in);
  134.         exit(-2);
  135.     }
  136.     while((c=getc(fin))!=EOF){
  137.         ungetc(c,fin);
  138.         fgets(buf,0x100,fin);
  139.         sscanf(buf,"%s %s %s %d %d %d %d %d",cmd,name,leadin,
  140.             &pins,&textinside,&drawnum,&units,&pinsperunit);
  141.         if((strcmp("DEF",cmd))==0){
  142.  
  143. /* see if we can find the part in the library */
  144.  
  145.             if((strcmp(name,file_out))==0){
  146.                 found_error=1;
  147.             }
  148.         }
  149.     }    /* checked the whole file, see if an error is called for */
  150.     fclose(fin);
  151.     if(found_error != 0){
  152.         fprintf(stderr,"%s exists in the library already\n",file_out);
  153.         fprintf(stderr,"Remove old part before installing new\n");
  154.         exit(-3);
  155.     }
  156.     sprintf(make_name,"%s.bit",file_out);
  157.     if((fin=fopen(file_in,"a"))==NULL){
  158.         fprintf(stderr,"Can't open library file %s\n",file_in);
  159.         exit(-2);
  160.     }
  161.     if((fout=fopen(make_name,"r"))==NULL){
  162.         fprintf(stderr,"Can't open part file %s\n",make_name);
  163.         exit(-2);
  164.     }
  165.  
  166.     printf("About to add %s to library %s\n",make_name,file_in);
  167.  
  168.      
  169. /* opened the library file at the end, and about to copy */
  170. /* new data to the end of library file */
  171.  
  172.     while((c=getc(fout))!=EOF)
  173.         putc(c,fin);
  174.     
  175. /* close all the files */
  176.     fclose(fin);
  177.     fclose(fout);
  178. }
  179. void extract_lib(void)
  180. {
  181.     char output[0x20];
  182.  
  183.     if((fin=fopen(file_in,"r"))==NULL){
  184.         fprintf(stderr,"Can't open %s for reading\n",file_in);
  185.         exit(-2);
  186.     }
  187.     while((c=getc(fin))!=EOF){
  188.         ungetc(c,fin);
  189.         fgets(buf,0x100,fin);
  190.         sscanf(buf,"%s %s %s %d %d %d %d %d",cmd,name,leadin,
  191.             &pins,&textinside,&drawnum,&units,&pinsperunit);
  192.         if((strcmp("DEF",cmd))==0){
  193.  
  194. /* find the part in the library */
  195.  
  196.             if((strcmp(name,file_out))==0){
  197.                 printf("%s found in %s, Unpacking to %s.bit\n",
  198.                     file_out,file_in,file_out);
  199.                 sprintf(output,"%s.bit",file_out);
  200.                 if((fout=fopen(output,"w"))==NULL){
  201.                     fprintf(stderr,"Cant open %s\n",
  202.                         output);
  203.                     exit(-2);
  204.                 }
  205.                 copy_to_file();
  206.                 fclose(fout);
  207.             }
  208.         }
  209.     }
  210. }
  211. int copy_to_file(void)
  212. {
  213.     int flag = 1;
  214.     fprintf(fout,"%s",buf);
  215.     while(flag){
  216.         fgets(buf,0x100,fin);
  217.         sscanf(buf,"%s",name);
  218.         if((strcmp(name,"ENDDEF"))==0){
  219.             fprintf(fout,"%s",buf);
  220.             return(1);
  221.         } else {
  222.             fprintf(fout,"%s",buf);
  223.         }
  224.     }
  225.     return(0);    /* never reached */
  226. }
  227.     
  228. void create_lib(void)
  229. {
  230.     char answer;
  231.  
  232.     if((fout=fopen(file_in,"r"))==NULL){
  233.         create();
  234.     } else {
  235.         fclose(fout);
  236.         /* Should we create ?? */
  237.         printf("File Exist's, Overwrite (Y/N)? ");
  238.         answer=getch();
  239.         if(answer=='Y' | answer=='y')
  240.             create();
  241.     }
  242. }
  243. void create(void)
  244. {
  245.     if((fout=fopen(file_in,"w"))==NULL){
  246.         fprintf(stderr,"Can't write to %s\n",file_in);
  247.         exit(-2);
  248.     }
  249.     fprintf(fout,"EEDRAW-LIB Version 1\n#\n#\n# ");
  250.     fprintf(fout,"Created by EELib\n#\n#\n#\n\n"); 
  251.     fprintf(fout,"# Standard Layer Descriptions Follow\n#\n#\n");
  252.     fprintf(fout,"#\t0) Wire\t\tWire Layer\n");
  253.     fprintf(fout,"#\t1) Bus\t\tBus Layer\n");
  254.     fprintf(fout,"#\t2) Gate\t\tGate Outline Layer\n");
  255.     fprintf(fout,"#\t3) IEEE\t\tIEEE Outline Layer\n");
  256.     fprintf(fout,"#\t4) PinFun\tin function Layer\n");
  257.     fprintf(fout,"#\t5) PinNum\tPin Number Layer\n");
  258.     fprintf(fout,"#\t6) PinNam\tPin Name Layer\n");
  259.     fprintf(fout,"#\t7) RefDes\tReference Designator Layer\n");
  260.     fprintf(fout,"#\t8) Attr\t\tAttribute Layer\n");
  261.     fprintf(fout,"#\t9) Device\tDevice Name Layer\n");
  262.     fprintf(fout,"#\t10) Notes\tUser Notes Layer\n");
  263.     fprintf(fout,"#\t11) NetNam\tNetwork Name Layer\n");
  264.     fprintf(fout,"#\t12) Pin\t\tDevice Pin Layer\n");
  265.     fprintf(fout,"#\n#\n#\n");
  266.     fclose(fout);
  267.  
  268. }
  269. void delete_lib(void)
  270. {
  271.     char fn[0x100];
  272.     int found = 0;
  273.     printf("About to delete %s from library %s\n",file_out,file_in);
  274.     if((fin=fopen(file_in,"r"))==NULL){
  275.         fprintf(stderr,"Can't open file %s\n",file_in);
  276.         exit(-2);
  277.     }
  278.     if((ftmp=fopen(SCRATCH_FILE,"w"))==NULL){
  279.         fprintf(stderr,"Can't open scratch file\n");
  280.         exit(-2);
  281.     }
  282.     while((c=getc(fin))!=EOF){
  283.         ungetc(c,fin); 
  284.         fgets(buf,0x100,fin);
  285.         sscanf(buf,"%s %s %s %d %d %d %d %d",cmd,name,leadin,
  286.             &pins,&textinside,&drawnum,&units,&pinsperunit);
  287.         if((strcmp(name,file_out))==0){
  288.             found=1;
  289.             sprintf(fn,"%s.%s",file_out,DEL_FILE);
  290.             if((fout=fopen(fn,"a"))==NULL){
  291.                 fprintf(stderr,"Can't open delete file %s\n",
  292.                     fn);
  293.                 exit(-2);
  294.             }
  295.             copy_to_file(); 
  296.             fclose(fout);
  297.         } else 
  298.             fprintf(ftmp,"%s",buf);
  299.     } /* file copyed to the scratch file, now copy back to library */
  300.     fclose(ftmp);
  301.     fclose(fin);
  302.     replace_file(SCRATCH_FILE,file_in);
  303.     if(found==0){
  304.         fprintf(stderr,"%s Not found in library %s\n",file_out,file_in);
  305.     }
  306.     unlink(SCRATCH_FILE);    /* Clear the tempary file */
  307. }
  308. void replace_file(char *file1, char *file2)
  309. {
  310.     if((fin=fopen(file1,"r"))==NULL){
  311.         fprintf(stderr,"Can't open %s for reading\n",
  312.             SCRATCH_FILE);
  313.         exit(-2);
  314.     }
  315.     if((fout=fopen(file2,"w"))==NULL){
  316.         fprintf(stderr,"Can't open file %s for writting\n",file_in);
  317.         exit(-2);
  318.     }
  319.     while((c=getc(fin))!=EOF)
  320.         putc(c,fout);
  321.     fclose(fin);
  322.     fclose(fout);
  323.  
  324. }
  325. void show_lib(void)
  326. {
  327.     int cnt;
  328.     cnt=0;
  329.  
  330.     if((fin=fopen(file_in,"r"))==NULL){
  331.         fprintf(stderr,"Can't open %s for reading\n",file_in);
  332.         exit(-2);
  333.     }
  334.     while((c=getc(fin))!=EOF){
  335.         ungetc(c,fin);
  336.         fgets(buf,0x100,fin);
  337.         sscanf(buf,"%s %s %s %d %d %d %d %d",cmd,name,leadin,
  338.             &pins,&textinside,&drawnum,&units,&pinsperunit);
  339.         if((strcmp("DEF",cmd))==0){
  340.             if(cnt==0){
  341.                 printf("%s:\t%s",file_in,name);
  342.                 cnt++;
  343.             } else {
  344.                 printf("\t%s",name);
  345.                 cnt++;
  346.                 if(cnt==4){
  347.                     printf("\n");
  348.                     cnt=0;
  349.                 }
  350.             }
  351.         }
  352.     }
  353.     printf("\n");
  354.     fclose(fin);
  355. }
  356. void list_lib(void)
  357. {
  358.     if((fin=fopen(file_in,"r"))==NULL){
  359.         fprintf(stderr,"Can't open %s for reading\n",file_in);
  360.         exit(-2);
  361.     }
  362.     if((fout=fopen(file_out,"w"))==NULL){
  363.